//
//  MessageTableViewCell.m
//  51易借
//
//  Created by 张睿 on 2017/8/21.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "MessageTableViewCell.h"
#import "SystemListModel.h"

@interface MessageTableViewCell ()

@property (nonatomic, weak)UIImageView * leftImageView;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UIImageView * rightImageView;


@end

@implementation MessageTableViewCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}



- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIImageView * leftImageView = [UIImageView new];
    
    leftImageView.image = [UIImage imageNamed:@"点"];
    
    self.leftImageView = leftImageView;
    
    [self.contentView addSubview:self.leftImageView];
    
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(20 * SCREENH6);
        make.centerY.mas_equalTo(self);
        make.width.height.mas_equalTo(9 * SCREENH6);
    }];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"51易借正式上线";
    titleLabel.textColor = ColorWithRGB(0x303c41);
    titleLabel.font = [UIFont systemFontOfSize:13.5 * SCREENH6];
//    titleLabel.font = [UIFont fontWithName:TYPE_FACE size:12 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.contentView addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(10 * SCREENH6);
        make.leading.mas_equalTo(self.leftImageView.mas_trailing).offset(15 * SCREENH6);
    }];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.text = @"2016.02.02 23：21：23";
    timeLabel.textColor = ColorWithRGB(0x303c41);
    timeLabel.font = [UIFont systemFontOfSize:13.5 * SCREENH6];
//    timeLabel.font = [UIFont fontWithName:TYPE_FACE size:12 * SCREENH6];
    
    self.timeLabel = timeLabel;
    
    [self.contentView addSubview:self.timeLabel];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5 * SCREENH6);
        make.leading.mas_equalTo(self.titleLabel);
    }];
    
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"倒三角-箭头"];
    
    self.rightImageView = rightImageView;
    
    [self.contentView addSubview:self.rightImageView];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.contentView).offset(-13 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
}


- (void)willTransitionToState:(UITableViewCellStateMask)state{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (UIView *subView in self.subviews) {
            
            if ([NSStringFromClass([subView class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                UIView *view = ((UIView *)[subView.subviews firstObject]);
                
                view.backgroundColor = TOTAL_COLOR;
                view.superview.backgroundColor = TOTAL_COLOR;
                
                //替换字体
                [view.subviews[0] setValue:@"删除" forKey:@"text"];
                //替换字体颜色
                [view.subviews[0] setValue:[UIColor whiteColor] forKeyPath:@"textColor"];
                
                //也可以直接设置view.layer 但是不会出现边框跟着移动的效果(下图), 这也说明了, UITableViewCellDeleteConfirmationView的frame是跟着你的手指移动在变化的
//                view.superview.layer.cornerRadius = 10.0;
//                view.superview.layer.borderWidth = 2.0;
//                view.superview.layer.borderColor = [UIColor greenColor].CGColor;
//                view.superview.layer.masksToBounds = YES;
                
                
            }
        }
    });
    
}


- (void)setModel:(MessageListModel *)model{
    
    _model = model;
    
    
    if ([model.read boolValue]) {
        
        self.leftImageView.hidden = YES;
    }else{
        
        self.leftImageView.hidden = NO;
    }
    
    self.titleLabel.text = [NSString stringWithFormat:@"%@",model.name];
    self.timeLabel.text = [Lahelper formattedTimeFromTimeInterval:[model.createTime longLongValue]];
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
